package com.deve.pig.service.category.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import my.utils.Pagination;

import org.springframework.stereotype.Service;

import com.deve.pig.mapper.CategoryMapper;
import com.deve.pig.model.Category;
import com.deve.pig.model.CategoryExample;
import com.deve.pig.model.CategoryExample.Criteria;
import com.deve.pig.service.category.ICategoryService;

@Service("CategoryService")
public class CategoryServiceImpl implements
		ICategoryService
{
	@Resource(name = "categoryStatusHtml")
	private LinkedHashMap<String, String> categoryStatusHtml;

	@Resource
	private CategoryMapper CategoryMapper;

	@Override
	public int add(Category entity)
	{
		return CategoryMapper.insert(entity);
	}

	@Override
	public int delete(CategoryExample example)
	{
		return CategoryMapper.deleteByExample(example);
	}

	@Override
	public int delete(int id)
	{
		return CategoryMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int update(Category entity)
	{
		return CategoryMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public Pagination findPagination(Map<String, Object> queryMap,
			int currentPage, int pageSize)
	{
		CategoryExample CategoryExample = new CategoryExample();
		CategoryExample.setOrderByClause("id desc");
		Criteria criteria = CategoryExample.createCriteria();
		// 设置搜索条件参数
		if (queryMap != null)
		{
			if (queryMap.containsKey("pid"))
			{
				criteria.andPidEqualTo((Integer) queryMap.get("pid"));
			}
			if (queryMap.containsKey("name"))
			{
				criteria.andNameLike("%" + (String) queryMap.get("name") + "%");
			}
		}
		// 设置分页参数
		CategoryExample.setPageSize(pageSize);
		CategoryExample.setStartIndex((currentPage - 1) * pageSize);
		List<Category> items = CategoryMapper.selectByExample(CategoryExample);
		for(Category c : items)
		{
			c.setStatusStr(categoryStatusHtml.get(c.getStatus() + ""));
		}
		int totalCount = (int) CategoryMapper.countByExample(CategoryExample);
		return new Pagination(pageSize, currentPage, totalCount, items);
	}

	@Override
	public Category findById(int id)
	{
		return CategoryMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Category> findAll()
	{
		return CategoryMapper.selectByExample(null);
	}

	@Override
	public List<Category> findByCriteria(
			CategoryExample criteria)
	{
		List<Category> items = CategoryMapper.selectByExample(criteria);
		for(Category c : items)
		{
			c.setStatusStr(categoryStatusHtml.get(c.getStatus() + ""));
		}
		return items;
	}

	@Override
	@SuppressWarnings("unchecked")
	public LinkedHashMap<String, List> getExportData()
	{
		LinkedHashMap<String, List> map = new LinkedHashMap<String, List>();
		map.put("SheetName_1", findAll());
		map.put("SheetName_2", findAll());
		map.put("SheetName_3", findAll());
		map.put("SheetName_N", findAll());
		return map;
	}

	@Override
	public int findCountByPid(int id, List<Byte> status)
	{
		CategoryExample example = new CategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusIn(status);
		criteria.andPidEqualTo(id);
		return CategoryMapper.countByExample(example);
	}

	@Override
	public List<Category> findByPid(int id, List<Byte> status)
	{
		CategoryExample CategoryExample = new CategoryExample();
		Criteria criteria = CategoryExample.createCriteria();
		criteria.andStatusIn(status);
		criteria.andPidEqualTo(id);
		return findByCriteria(CategoryExample);
	}
}